package cn.com.shopec.erp.warehouse.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.financing.service.IWhareHouseServer;
import cn.com.shopec.erp.product.entity.ProductStockExt;
import cn.com.shopec.erp.product.service.IProductStockService;
import cn.com.shopec.erp.product.vo.ProductStockVo;
import cn.com.shopec.erp.stock.commo.JsonUtil;
import cn.com.shopec.erp.stock.controller.StockOutController;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.service.IUserService;
import cn.com.shopec.erp.system.vo.RoleVo;
import cn.com.shopec.erp.warehouse.entity.StockTransfer;
import cn.com.shopec.erp.warehouse.entity.StockTransferProduct;
import cn.com.shopec.erp.warehouse.service.IStockTransferService;
import cn.com.shopec.erp.warehouse.vo.StockTransferVo;

/**
 * 货品过期控制层
 * 
 * @author Jack
 *
 */
@Controller
@RequestMapping("/warehouse/stockTransferController")
public class StockTransferController {

	@Resource
	private IStockTransferService stockTransferService;

	@Resource
	private IWhareHouseServer whareHouseServer;

	@Resource
	private IProductStockService productStockService;
	@Resource
	private IUserService userService;

	/** 日志. */
	private Logger log = Logger.getLogger(StockOutController.class);

	/**
	 * 分页查询调拨信息
	 * 
	 * @param packageInfo
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("ajaxPageFinderStockTransfer")
	@ResponseBody
	public String ajaxPageFinderStockTransfer(StockTransferVo stockTransfer, PageQuery pageQuery, ModelMap map) {
		JSONObject jsonObj = new JSONObject();
		PageFinder<StockTransfer> pageFinder = stockTransferService.queryStockTransferList(stockTransfer, pageQuery);
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}

	/**
	 * 分页查询调拨商品信息
	 * 
	 * @param packageInfo
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("ajaxPageFinderStockTransferProduct")
	@ResponseBody
	public String ajaxPageFinderStockTransferProduct(StockTransferVo stockTransfer, PageQuery pageQuery, ModelMap map) {
		JSONObject jsonObj = new JSONObject();
		PageFinder<StockTransferProduct> pageFinder = stockTransferService.queryStockTransferProductList(stockTransfer,
				pageQuery);
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}

	/**
	 * 跳转首页
	 * 
	 * @param stockHistory
	 * @param pageQuery
	 * @param map
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("pageFinderStockTransfer")
	public String pageFinderStockTransfer(StockTransferVo stockTransfer, PageQuery pageQuery, ModelMap map) throws Exception {
		List<RoleVo> roleVos = this.userService.getOwnRoleVos(UserUtils.getUserVo());
		map.put("whareHouse", whareHouseServer.getPagedCollectSetList(pageQuery).getData());
		map.put("roleVos", roleVos);
		map.put("stockTransfer", stockTransfer);
		return "/stock/stock_transfer_list";
	}

	/**
	 * 跳转详情
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("toStockTransferDetail")
	public String toStockTransferDetail(String tabId, String id, ModelMap map) throws Exception {
		if (!StringUtil.isEmpty(id)) {
			map.put("stockTransfer", stockTransferService.getStockTransferById(id));
			map.put("tabId", tabId);
		}
		return "/stock/stock_transfer_detail";
	}

	/**
	 * 删除产品
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("deleteStockTransfer")
	@ResponseBody
	public String deleteStockTransfer(String id) {
		try {
			if (stockTransferService.deleteStockTransfer(id)) {
				return SZSCMConstant.SUCCESS;
			}
		} catch (Exception e) {
			log.error("deleteStockTransfer" + e.getMessage(), e);
		}
		return SZSCMConstant.FAIL;
	}

	/**
	 * 跳转新增
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("toAddStockTransfer")
	public String toAddStockTransfer(String tabId, ModelMap map) throws Exception {
		map.put("tabId", tabId);
		map.put("whareHouse", whareHouseServer.getAllWhareHouse());
		return "/stock/stock_transfer_add";
	}

	/**
	 * 新增入库信息
	 * 
	 * @param stockIn
	 * @param products
	 * @param map
	 * @return
	 */
	@RequestMapping("addStockTransfer")
	@ResponseBody
	public String addStockTransfer(StockTransfer stockTransfer, String products, ModelMap map) {
		try {
			@SuppressWarnings("unchecked")
			List<StockTransferProduct> stockTransferProducts = (List<StockTransferProduct>) JsonUtil
					.changeJsonToList(products, StockTransferProduct.class);
			if (stockTransferService.addStockTransfer(stockTransfer, stockTransferProducts)) {
				return SZSCMConstant.SUCCESS;
			}
		} catch (Exception e) {
			log.error("addStockTransfer" + e.getMessage(), e);
		}
		return SZSCMConstant.FAIL;
	}

	/**
	 * 审核
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("approveStockTransfer")
	@ResponseBody
	public String approveStockTransfer(String transferNo, ModelMap map) throws Exception {
		try {
			if (stockTransferService.setApproveFlowState(transferNo)) {
				return SZSCMConstant.SUCCESS;
			}
		} catch (Exception e) {
			log.error("addStockInfo" + e.getMessage(), e);
		}
		return SZSCMConstant.FAIL;
	}

	/**
	 * 跳转选择库存页面
	 * 
	 * @param whNo
	 * @param map
	 * @return
	 */
	@RequestMapping("/toQueruyStockProduct")
	public String toQueruyStockProduct(String whNo, ModelMap map) {
		map.put("whNo", whNo);
		return "/stock/stock_transfer_select";
	}

	/**
	 * 查询库存
	 * 
	 * @param productStockVo
	 * @param pageQuery
	 * @return
	 */
	@RequestMapping("/queruyStockProduct")
	@ResponseBody
	public String queruyStockProduct(ProductStockVo productStockVo, PageQuery pageQuery) {
		PageFinder<ProductStockExt> page = productStockService.productStockPaged(productStockVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}
}
